import React, { Component } from 'react';
import './App.css';
import LocalStore from './util/localStore';
import {CITYNAME} from "./config/localStoreKey";
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as userInfoActionsFormOtherFile from './actions/userinfo';

// import Hello from "./containers/Hello/index";
// import Todo from './containers/Todo/index';
// import Perf from 'react-addons-perf';

// 引用并执行 redux-demo
// import fn from './redux-demo'
// fn()

class App extends Component {
  constructor(props, context){
    super(props, context);
    this.state = {
      initDone: false
    }
  }

  render() {
    return (
      <div>
        {
          this.state.initDone
          ? this.props.children
            : <div>正在加载...</div>
        }
      </div>
    );
  }

  componentDidMount(){
    // TODO 深入理解javascript的闭包和原型
    // console.log(this);
    // var that = this;
    // 获取位置信息
    // 从localstorage里面获取城市
    let cityName = LocalStore.getItem(CITYNAME);
    if (cityName == null) {
      cityName = '深圳'
    }

    // 将城市信息存储到Redux中
    this.props.userInfoActions.update({
      cityName: cityName
    });
    // 更改状态
    // setTimeout(()=> {
    //   console.log(this);
    //   this.setState({
    //     initDone: true
    //   });
    // }, 1000);
    this.setState({
      initDone: true
    });
  }

}

function mapStateToProps(state) {
  return {

  }
}

function mapDispatchToProps(dispatch) {
  return {
    userInfoActions: bindActionCreators(userInfoActionsFormOtherFile, dispatch)
  }
}

export default connect(
  mapStateToProps,
  mapDispatchToProps)
(App);
